from qiniu import Auth,put_data

# 需要填写你的 Access Key 和 Secret Key
access_key = 'yV4GmNBLOgQK-1Sn3o4jktGLFdFSrlywR2C-hvsW'
secret_key = 'bixMURPL6tHjrb8QKVg2tm7n9k8C7vaOeQ4MEoeW'

# 要上传的空间
bucket_name = 'ihome'

def storage(data):


    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)

        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name)
        # 上传文件,把key删除写成None,他会自动生成,上传成功的时候返回给我们
        ret, info = put_data(token, None, data)
        print(ret,info)
    except Exception as e:
        raise e;

    if info.status_code !=200:
        raise Exception("上传图片失败")


    return ret["key"]


if __name__ == '__main__':

    #文件路径:/home/python/Desktop/rose.jpeg
    file = input("请输入文件路径")
    with open(file, "rb") as f:
        storage(f.read())